//
//  WBStatusViewModel.swift
//  微博
//
//  Created by lipengfei on 15/9/8.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit
/// 原创微博
let WBStatusNormalCellID = "WBStatusNormalCellID"
///转发的微博
let WBStatusForwardCellID = "WBStatusForwordCellID"
///  一条微博的视图模型
class WBStatusViewModel: NSObject {
    var status: WBStatus
    ///记录行高
    var rowHeight:CGFloat = 0
    /// 初始化
    init(status:WBStatus) {
        self.status = status
        print(self.status)
        if let urls = status.pic_urls{
//            print(status.pic_urls)
            thumbnailURLs = [NSURL]()
//            print(urls)
            for dict in urls{
                
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
//            print(thumbnailURLs)
        }
        if let url = status.retweeted_status?.pic_urls {
            thumbnailURLs = [NSURL]()
            for dict in url{
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        }
        super.init()
    }
    /// 返回当前视图模型对应的可重用标识符
    var cellID:String{
        
        return status.retweeted_status != nil ? WBStatusForwardCellID : WBStatusNormalCellID
    }
    //用户的头像
    var userIconUrl:NSURL?{
        //如果字符串为空，返回空
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    // 昵称
    var name:String?{
        return status.user?.name
    }
    // 会员等级
    var userMemberImage:UIImage?{
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    /// 认证类型 -1 没有认证 0 认证用户， 235 企业认证 220 达人
    /// imageWithNamed 方法能够缓存图片，所以两个计算型属性的效率不会受到影响
    ///设置计算型属性的时候，需要考虑性能
    /// imageWithNamed方法 不要加载太大的图片（缓存到本地），程序员我发释放内存
    var userVipImage: UIImage?{
        switch (status.user?.verified ?? -1)
        {
        case 0 :return UIImage(named: "avatar_vip")
        case 2,3,5:return UIImage(named: "avatar_enterprise_vip")
        case 220 : return UIImage(named: "avatar_grassroot")
        default :return nil
        }
    }
    //转发微博的文字
    var forwardText:String?{
        let username = status.retweeted_status?.user?.name ?? ""
        let text = status.retweeted_status?.text ?? ""
        return "@\(username):\(text)"
    }
    // 配图缩略图 url数组
    var thumbnailURLs:[NSURL]?
    
///  对象描述
    override var description:String{
        return status.description + "缩略图URL数组\(thumbnailURLs)"
    }
}
